<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // rest参数
        // ES5写一个函数，长度不定
        function sum(){
            var sum = 0;
            for(i=0;i<arguments.length;i++){
                sum += arguments[i];
            }
            console.log(sum);
        }
        sum(1,2);
        sum(2,3,4,5,6)

        // ES6中用剩余参数实现不定长度的函数
        // args是数组
        function sum1(...args){
            console.log(args);
            let sum = 0;
            args.forEach(function(item){
                sum += item;
            })
            console.log(sum)
        }
        sum1(1,2,3);
        sum1(4,5,6,7,8);

        // 剩余参数比arguments更加灵活
        function fn(name,...args){
            console.log(name,args);//jack,//10,20,男,30000
            console.log(arguments);//jack,10,20,男,30000
        }    
        fn("jack",10,20,"男",30000)
    </script>
</body>
</html>